package com.buss.purchase.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.purchase.dao.PurchaseOrderDetailDao;
import com.buss.purchase.model.PurchaseOrderDetail;

@Repository
public class PurchaseOrderDetailDaoImpl implements PurchaseOrderDetailDao {

	@Autowired
	private BaseDao<PurchaseOrderDetail> baseDao;
	
	/**
	 * 保存订单明细信息
	 * @param detail
	 */
	public void saveOrderDetail(PurchaseOrderDetail detail){
		baseDao.save(detail);
	}
	
	/**
	 * 修改订单明细信息
	 * @param detail
	 */
	public void editOrderDetail(PurchaseOrderDetail detail){
		baseDao.edit(detail);
	}
	
	@Override
	public void saveOrderDetails(List<PurchaseOrderDetail> orderDetails) {
		for(PurchaseOrderDetail detail : orderDetails){
			baseDao.save(detail);
		}
	}

	@Override
	public void editOrderDetails(List<PurchaseOrderDetail> orderDetails) {
		for(PurchaseOrderDetail detail : orderDetails){
			baseDao.saveOrEdit(detail);
		}
	}

	@Override
	public void removeOrderDetail(Long[] ids) {
		String sql = "delete from PurchaseOrderDetail detail where detail.id in (:ids)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		baseDao.excuteHsql(sql, params);
	}

	public void removeOrderDetailByMasterId(Long masterId){
		String sql = "delete from PurchaseOrderDetail detail where detail.masterId = :masterId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("masterId", masterId);
		baseDao.excuteHsql(sql, params);
	}
	
	/**
	 * 获取指定的订单商品详情信息
	 * @param id
	 * @return
	 */
	public PurchaseOrderDetail loadOrderDetail(Long id){
		return baseDao.get(id, PurchaseOrderDetail.class);
	}
	
	/**
	 * 根据单位编码、订单号和商品ID获取订单详情信息
	 * @param companyId
	 * @param orderNumber
	 * @param goodsId
	 * @return
	 */
	public PurchaseOrderDetail loadOrderDetail(String companyId,Long masterId,Long goodsId){
		String sql = "select detail from PurchaseOrderDetail detail where detail.goods.goods.category.companyId = :companyId " +
				"and detail.masterId = :masterId and detail.goods.id = :goodsId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("masterId", masterId);
		params.put("goodsId", goodsId);
		return baseDao.loadHsql(sql, params);
	}
	
	@Override
	public ResultList<PurchaseOrderDetail> queryOrderDetail(Long masterId,
			Long categoryId, Long goodsId,Paginalbe page) {
		String sql = "select detail from PurchaseOrderDetail detail where detail.masterId = :masterId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("masterId", masterId);
		if(categoryId != null){
			sql += " and detail.goods.goods.category.id = :categoryId ";
			params.put("categoryId", categoryId);
		}
		if(goodsId != null){
			sql += " and detail.goods.goods.id = :goodsId ";
			params.put("goodsId", goodsId);
		}
		return baseDao.queryHsql(sql, params, page);
	}

}
